package com.jysz.certificate.web.action.report;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.jysz.certificate.action.AbstractBaseAction;
import com.jysz.certificate.persistence.enums.PostType;
import com.jysz.certificate.persistence.model.system.Brand;
import com.jysz.certificate.persistence.model.system.Financial;
import com.jysz.certificate.persistence.model.system.Organization;
import com.jysz.certificate.persistence.model.system.Shop;
import com.jysz.certificate.service.cm.CertificateService;
import com.jysz.certificate.service.system.BrandService;
import com.jysz.certificate.service.system.FinancialService;
import com.jysz.certificate.service.system.OrganizationService;
import com.jysz.certificate.service.system.ShopService;
import com.jysz.webcore.utils.ReflectionUtils;

@Controller
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@Namespace("/report")
public class ReportAction extends AbstractBaseAction  {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Resource
	private BrandService brandService;//渠道
	@Resource
	private CertificateService certificateService;//合格证
	@Resource
	private ShopService shopService;//销售店
	@Resource
	private FinancialService financialService;//金融机构
	@Resource
	private OrganizationService organizationService;
	
	private List<Brand> brands ;//渠道
	private List<Financial> financials;//金融机构
	private List<Shop> shops;
	private Date date;
	private Shop defaultShop;
	
	private String shopStr;
	
	/**
	 * 银行日报表 
	 * @return
	 */
	@Action("bankDayReport")
	public String bankDayReport(){
		this.financialList();
//		if(PostType.Shop.equals(getUser().getPost())){
			 List<Organization> orgs = organizationService.findShopsByCurrentUser();
			 shops = new ArrayList<Shop>();
			 if(orgs != null && orgs.size() > 0){
					for (Iterator<Organization> iterator = orgs.iterator(); iterator.hasNext();) {
						Organization org = iterator.next();
						Shop shop = shopService.find(org.getObjectId());
						shops.add(shop);
					}
		   	}
			 
			if(shops != null && shops.size() == 1){
				defaultShop = shops.get(0);
			}
			
//		}
		 this.setDate(new Date());
		return "bankDayReport";
	}
	/**
	 * 年度管理费明细表 
	 * @return
	 */
	@Action("control")
	public String control(){
		this.brandList();
		return "control";
	}
	
	
	/**
	 * 风险管理费_监管月份
	 * @return
	 */
	@Action("billMonth")
	public String billMonth(){
		shopStr = "-1" ;
		if(this.getUser() != null 
				&& (PostType.Admin.equals(this.getUser().getPost()) || PostType.Responsible.equals(this.getUser().getPost()))){
			shopStr = "";
		}else{
			 List<Organization> orgs = organizationService.findShopsByCurrentUser();
			 if(orgs != null && orgs.size() > 0){
				 shopStr = ReflectionUtils.convertElementPropertyToString(orgs, "objectId", ",");
		   	}
		}
		
		return "billMonth";
	}
		
	/**
	 * 帐龄分析表
	 * @return
	 */
	@Action("finance")
	public String finance(){
		this.brandList();
		this.financialList();
		return "finance";
	}
	/**
	 * 库存信息汇总表 
	 * @return
	 */
	@Action("storage")
	public String storage(){
		this.financialList();
		
		return "storage";
	}
	/**
	 * 获得所有的渠道
	 */
	public void brandList(){
		brands = brandService.findAll();
	}
	/**
	 * 获得所有的金融机构
	 */
	public void financialList(){
		financials = financialService.findAvailableAll();
	}
	public List<Brand> getBrands() {
		return brands;
	}
	public void setBrands(List<Brand> brands) {
		this.brands = brands;
	}
	public List<Financial> getFinancials() {
		return financials;
	}
	public void setFinancials(List<Financial> financials) {
		this.financials = financials;
	}
	public List<Shop> getShops() {
		return shops;
	}
	public void setShops(List<Shop> shops) {
		this.shops = shops;
	}
	public Date getDate() {
		return date;
	}
	public void setDate(Date date) {
		this.date = date;
	}
	public Shop getDefaultShop() {
		return defaultShop;
	}
	public void setDefaultShop(Shop defaultShop) {
		this.defaultShop = defaultShop;
	}
	public String getShopStr() {
		return shopStr;
	}
	public void setShopStr(String shopStr) {
		this.shopStr = shopStr;
	}
	
}
